<?php
/**
	 * 连接数据库，如果连接成功返回一个mysqli对象，否则返回null
	 * 这边有几个问题需要说明：
	 * 连接的是本地数据库；
	 * 用户名是root，密码为空，数据库名称是goods，这些到时候需要统一一下
	 * Enter description here ...
	 */

	function db_connect()
	{
		$db_con  = new mysqli("localhost", "root","root", "goods");
		if(mysqli_errno())
		{
			return null;
		}
		return $db_con;
	}
	
	
	/**
	 * 这个方法的作用是根据parenttype的id来找出所有的子类别的id
	 * @param unknown_type $fatherType
	 * 返回值是一个存放了所有catalog类别id的数组
	 */
	
	
	function getCategories($fatherType)
	{
		$db_con = db_connect();
		$query = "select gtypeid from goods_type where fathertype = $fatherType ";
		$result = $db_con->query($query);
		$types = array();
		$rownums = $result->num_rows;
		
		for($i = 0 ; $i < $rownums ; $i ++)
		{
			$row = $result->fetch_assoc();
			$types[$i] = $row["gtypeid"];
		}
		
		$db_con->close();
		return $types;
	}
	
	
	/**
	 * 函数的作用是根据商品类型的id，获取类别的名称
	 * @param unknown_type $typeid
	 */
	function get_typename($typeid)
	{
		$db_con = db_connect();
		$query = " select gtypename from goods_type where gtypeid = $typeid ";
		//echo $query;
		$result = $db_con->query($query);
		$row = $result->fetch_assoc();
		$db_con->close();
		return $row["gtypename"];
	}
	
	function get_goods($typeid)
	{
		//echo $typeid;
		$db_con = db_connect();
		$query = "select gid from goods_moreinfo where gtypeid = $typeid";
		$result = $db_con->query($query);
		$goodsid = array();
		$rownum = $result ->num_rows;
		//echo "rownum is : $rownum";
		for($index = 0; $index < $rownum; $index ++)
		{
			$row = $result->fetch_assoc();
		    $goodsid [$index] = $row["gid"];
		}
		$db_con->close();
		return $goodsid;
	}
	
	function get_imagepath($gid)
	{
		$db_con = db_connect();
		$query = "select imagepath from goods_image where goodsid = $gid";
		$result = $db_con->query($query);
		$row = $result->fetch_assoc();
		$db_con->close();
		return $row["imagepath"];
	}
	
	function get_goodsbinfo($gid)
	{
		//echo "gid is : $gid";
		$db_con = db_connect();
		$query = "select* from goods_baseinfo where gid = $gid";
		$result = $db_con->query($query);
		$row = $result -> fetch_assoc();
		return $row;
	}
	
	?>